package pikes.peak;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.junit.Test;
import org.springframework.web.servlet.support.RequestContext;

import pikes.html.xhtml.Anchor;
import pikes.html.xhtml.Inline;
import pikes.html.xhtml.PCData;
import pikes.html.xhtml.text.phrase.Strong;
import pikes.peak.test.AssertHtml;
import pikes.peak.test.PikesPeakComponentTestCase;
import pikes.uri.RelativeURI;
import pikes.uri.URI;

public class LabelTestCase extends PikesPeakComponentTestCase {

	@Test
	public void labelWithSimpleText() throws Exception {
		Label label = new Label("Some text");

		Inline expected = new PCData("Translated Text");
		Inline actual = label.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}

	@Test
	public void labelWithSimpleText1() throws Exception {
		Label label = new Label();
		label.addText("Some text");

		Inline expected = new PCData("Translated Text");
		Inline actual = label.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}

	@Test
	public void labelWithSeveralTexts() throws Exception {
		Label label = new Label("Some text");
		label.addText(null);

		Inline expected = new PCData("Translated Text");
		Inline actual = label.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}

	@Test
	public void labelWithSeveralTexts1() throws Exception {
		messageSource.addMessage("New text", Locale.ENGLISH, "New Translated Text");

		Label label = new Label("Some text");
		label.addText("New text");

		Inline expected = new PCData("Translated TextNew Translated Text");
		Inline actual = label.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}

	@Test
	public void labelWithSimpleTextwithEnglishLocale() throws Exception {
		Label label = new Label("Some text");

		Inline expected = new PCData("Translated Text");
		Inline actual = label.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}

	@Test
	public void labelWithStyle() throws Exception {
		Label label = new Label("Some text");

		MockLabelStyle labelStyle = new MockLabelStyle();
		labelStyle.result = new Strong("Strong text");

		label.addStyle(labelStyle);

		Inline actual = label.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());

		AssertHtml.assertEquals(new Strong("Strong text"), actual);
		AssertHtml.assertEquals(new PCData("Translated Text"), labelStyle.parameter);
	}

	@Test
	public void labelWithStyle1() throws Exception {
		MockLabelStyle labelStyle = new MockLabelStyle();
		labelStyle.result = new Strong("Strong text");

		Label label = new Label("Some text", labelStyle);

		Inline actual = label.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());

		AssertHtml.assertEquals(new Strong("Strong text"), actual);
		AssertHtml.assertEquals(new PCData("Translated Text"), labelStyle.parameter);
	}

	@Test
	public void labelWithLink() throws Exception {
		Label label = new Label("Some text");
		/*
		 * label.setURI(new URIBuilder() { @Override protected void buildContextPath(Appendable string, Map model,
		 * RequestContext requestContext) throws Exception { string.append("/domain"); } @Override protected void
		 * buildPath(Appendable string, Map model, RequestContext requestContext) throws Exception {
		 * string.append("/path"); } @Override protected void buildScheme(Appendable uriString, Map model,
		 * RequestContext requestContext) throws Exception { // TODO Auto-generated method stub } });
		 */
		label.setURI(new RequestProperty<URI>() {

			public URI getValue(Map model, RequestContext requestContext) throws Exception {
				return new RelativeURI("domain/path");
			}

		});

		Inline expected = new Anchor(new RelativeURI("domain/path"), "Translated Text");
		Inline actual = label.createHtml(new HashMap(), pikesPeakRequest.getRequestContext());
		AssertHtml.assertEquals(expected, actual);
	}
}
